package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.dto.UserLoginDTO;
import com.jumi.microservice.domain.dto.UserTokenDTO;
import com.jumi.microservice.domain.vo.UserLoginVO;
import com.jumi.microservice.domain.vo.UserTokenVO;
import com.jumi.microservice.service.UserLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 账号登录管理controller组件
 *
 * @author Administrator
 */
@Api(tags = {"后台用户登录管理相关接口"})
@RestController
@RequestMapping("/admin")
public class AdminController {

    /**
     * 账号登录管理模块的service组件
     */
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 用户登录
     *
     * @param userLogin 用户登录信息vo
     * @return 用户token
     */
    @PostMapping("/login")
    @ApiOperation("登录接口")
    public ResponseResult<UserTokenVO> login(@Validated @RequestBody UserLoginVO userLogin) {
        UserTokenDTO userToken = userLoginService.login(convertUserVo2Dto(userLogin));
        return ResultGenerator.genSuccessResult(convertAccountDto2Vo(userToken));
    }

    /**
     * 用户退出登陆
     *
     *@return 处理结果,退出登陆成功返回true
     */
    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public ResponseResult<Boolean> logout(HttpServletRequest request) {
        String uid = request.getHeader("uid");
        boolean logout = userLoginService.logout(Long.valueOf(uid));
        return ResultGenerator.genSuccessResult(logout);
    }

    /**
     * 用户修改登录密码
     *
     * @return 处理结果,修改登录密码成功返回true
     */
    @PostMapping("/resetPwd")
    @ApiOperation("重置密码")
    public ResponseResult<Boolean> resetPwd(@Validated @RequestParam("password") @NotNull(message = "用户密码不能为空") String password,
                                            HttpServletRequest request) {
        String uid = request.getHeader("uid");
        boolean resetPwd = userLoginService.resetPwd(uid, password);
        return ResultGenerator.genSuccessResult(resetPwd);
    }

    /**
     * 将账号登录VO对象转换为DTO对象
     *
     * @param userLogin 账号VO
     * @return AccountDTO 账号DTO
     */
    private UserLoginDTO convertUserVo2Dto(UserLoginVO userLogin) {
        UserLoginDTO targetAccount = null;
        try {
            targetAccount = userLogin.clone(UserLoginDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetAccount;
    }

    /**
     * 将用户token DTO对象转换为VO对象
     *
     * @param userToken 用户token DTO
     * @return UserTokenVO 用户token vo
     */
    private UserTokenVO convertAccountDto2Vo(UserTokenDTO userToken) {
        UserTokenVO targetAccount = null;
        try {
            targetAccount = userToken.clone(UserTokenVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return targetAccount;
    }
}
